import type { InternalNamePath, Store } from 'antd/lib/form/interface';
import type { ITjFormItemProps } from './TjFormItem';
import _ from 'lodash';

const formItemDefaultLayout = {
  horizontal: {
    labelCol: {
      xs: 24,
      sm: 24,
      md: 5,
      lg: 5,
      xl: 3,
      xxl: 3,
    },
    wrapperCol: {
      xs: 24,
      sm: 24,
      md: 18,
      lg: 15,
      xl: 12,
      xxl: 12,
    },
  },
  vertical: {
    labelCol: { span: 24 },
    wrapperCol: { span: 24 },
  },
  inline: {
    labelCol: {
      xs: 24,
      sm: 24,
      md: 5,
      lg: 5,
      xl: 3,
      xxl: 3,
    },
    wrapperCol: {
      xs: 24,
      sm: 24,
      md: 18,
      lg: 15,
      xl: 12,
      xxl: 12,
    },
  },
};

// 不用XXX组件.defaultProps 最新版react会废除
const TjFormDefaultProps = {
  layout: 'horizontal',
  useGrid: {
    row: {
      gutter: 16,
    },
    col: {
      span: 12,
    },
  },
};

const formUtils = {
  /**
   * 主要用于设置FormOItem的Key
   *
   */
  getItemKey: (item: ITjFormItemProps, index?: number): string => {
    if (item.name) {
      if (_.isString(item.name)) {
        return item.name as string;
      }
      if (_.isArray(item.name)) {
        return (item.name as InternalNamePath).join('');
      }
      return item.name.toString();
    }
    if (index == null) {
      return '';
    }
    return index.toString();
  },

  handleInitialValues: (
    initialValues: Store | undefined,
    items: ITjFormItemProps[],
  ): Store | undefined => {
    if (
      initialValues &&
      _.isObject(initialValues) &&
      Object.keys(initialValues).length > 0 &&
      _.isArray(items) &&
      items.length > 0
    ) {
      const result: any = _.cloneDeep(initialValues);
      for (let i = 0; i < items.length; i++) {
        const item = items[i];
        if (item && _.isString(item.name)) {
          const names = (item.name as string).split('|');
          if (names.length > 1) {
            const obj: any = {};
            for (let i = 0; i < names.length; i++) {
              obj[names[i]] = result[names[i]];
              delete result[names[i]];
            }
            result[item.name as string] = obj;
          }
        }
      }

      return result;
    }
  },

  /**
   * 处理表单值
   * @param values 表单值
   * @param keepEmptyItem 是否保留字段值为空的字段
   * @param useTrim 是否去掉前后空格，仅对 string 类型的字段值有效性
   */
  handleValues: (values: Store, keepEmptyItem: boolean = false, useTrim: boolean = true) => {
    let items: Store = {};
    for (const key in values) {
      // 正常来说带｜的一般只用于select和RangePicker只要有值不存在去除空字符一说
      if (key.indexOf('|') > -1) {
        items = _.merge(items, values[key]);
      } else {
        if (keepEmptyItem && !values[key]) {
          items[key] = values[key];
        }
        if (values[key] || values[key] === 0) {
          items[key] = useTrim && _.isString(values[key]) ? values[key].trim() : values[key];
        }
      }
    }
    return items;
  },
};

export { TjFormDefaultProps, formItemDefaultLayout, formUtils };
